package io.renren.modules.dangan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.renren.common.utils.Constant;
import io.renren.modules.dangan.bean.FileMoveService;
import io.renren.modules.dangan.bean.FileMoverRunnable;
import io.renren.modules.dangan.dao.DaJnmlbDao;
import io.renren.modules.dangan.entity.DaJnmlbEntity;
import io.renren.modules.dangan.entity.DaServerEntity;
import io.renren.modules.dangan.service.DaJnmlbService;
import io.renren.modules.dangan.service.DaServerService;
import io.renren.modules.dangan.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.FutureTask;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.dangan.dao.DaWsDao;
import io.renren.modules.dangan.entity.DaWsEntity;
import io.renren.modules.dangan.service.DaWsService;
import org.springframework.transaction.annotation.Transactional;


@Service("daWsService")
@Slf4j
public class DaWsServiceImpl extends ServiceImpl<DaWsDao, DaWsEntity> implements DaWsService {

    @Autowired
    private DaJnmlbService daJnmlbService;
    @Autowired
    private DaServerService daServerService;
    @Value("${wsServerId:8}")
    private Integer wsServerId;

    @Value("${oaServerXh:7}")
    private String oaServerXh;
    @Autowired
    private DaJnmlbDao daJnmlbDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String js = (String) params.getOrDefault("js", "");
        Integer jh = 0;
        if (NumberUtil.isNumber(js)) {
            jh = Integer.parseInt(js);
        }
        String bgqx = (String) params.getOrDefault("bgqx","");
        String nd = (String) params.getOrDefault("nd","");
        String wh = (String) params.getOrDefault("wh","");
        String bt = (String) params.getOrDefault("bt","");
        String flh = (String) params.getOrDefault("flh","");
        Integer vrecid =  Integer.parseInt(params.getOrDefault("vrecid","0").toString());
        IPage<DaWsEntity> page = this.page(
                new Query<DaWsEntity>().getPage(params),
                new QueryWrapper<DaWsEntity>()
                        .eq(StringUtils.isNotEmpty(js) && NumberUtil.isNumber(js)
                                , "js", jh)
                        .like(StringUtils.isNotBlank(bgqx), "bgqx", bgqx.trim())
                        .like(StringUtils.isNotBlank(nd), "nd", nd.trim())
                        .like(StringUtils.isNotBlank(wh), "wh", wh.trim())
                        .like(StringUtils.isNotBlank(bt), "bt", bt.trim())
                        .like(StringUtils.isNotBlank(flh), "pflh", flh)
                        .like(vrecid>0, "vrecid", vrecid)
                .orderByAsc("js")
        );

        return new PageUtils(page);
    }

    @Override
    public Integer getJs(Map<String, Object> params) {
        String nd = params.get("nd").toString();
        String bgqx = params.get("bgqx").toString();
        params.put(Constant.LIMIT, "1");
        IPage<DaWsEntity> page = this.page(
                new Query<DaWsEntity>().getPage(params)
                , new QueryWrapper<DaWsEntity>()
                        .eq(StringUtils.isNotBlank(nd), "nd", nd.trim())
                        .eq(StringUtils.isNotBlank(bgqx), "bgqx", bgqx.trim())
                        .orderByDesc("js")
        );
        if(page.getRecords().size()==0){
            return 1;
        }
        return page.getRecords().get(0).getJs() + 1;
    }

    @Override
    public void delete(Map<String, Object> params) {
        String nbxh= (String) params.getOrDefault("nbxh","");
        if(StringUtils.isNotEmpty(nbxh)){
            daJnmlbDao.delete(new UpdateWrapper<DaJnmlbEntity>().eq("nbxh",nbxh));
            this.baseMapper.delete(new UpdateWrapper<DaWsEntity>().eq("nbxh",nbxh));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DaWsEntity guiDang(Map<String, Object> params) {
        FutureTask<Boolean> moveFileTask;
        DaServerEntity daServerEntity = daServerService.getById(wsServerId);
        DaWsEntity daWsEntity = BeanUtil.mapToBean(params, DaWsEntity.class, true);
        daWsEntity.setNbxh(IdUtil.createSnowflake(1, 1).nextIdStr());
        daWsEntity.setCreatedate(new Date());
        daWsEntity.setRq(DateUtil.parse(params.get("rq").toString()));
        daWsEntity.setSzd("市局");
        this.save(daWsEntity);
        DaServerEntity oaServer=null;
        File srcFile;
        String srcFilePath=params.get("filePath").toString();

        String srcFileName;
        if(params.get("type").toString().equals(Constant.gdType.OA)){
            srcFileName=srcFilePath.substring(srcFilePath.lastIndexOf("/")+1,srcFilePath.length());
        }else{
            srcFile=new File(srcFilePath);
            srcFileName=srcFile.getName();
        }
        String destFilePath = daServerEntity.getDiskname()+
                daServerEntity.getRelativepath()
                + FileUtils.getFilePath()+daWsEntity.getNbxh()+"/"
                + srcFileName;
        if(params.get("type").toString().equals(Constant.gdType.OA)){
            oaServer=daServerService.getById(
                    Integer.parseInt(oaServerXh));
            String oaPath=srcFilePath.substring(0,srcFilePath.lastIndexOf("/")+1);
            log.info("oaftp的目标路径是"+oaPath);
            FileMoverRunnable fileMoverRunnable=new FileMoverRunnable(
                    oaServer,srcFileName,oaPath,destFilePath
            );
            moveFileTask=new FutureTask<Boolean>(fileMoverRunnable);
            FileMoveService.getInstance().move(moveFileTask);

        }else{
            srcFile=new File(srcFilePath);

            File wsFile = new File(destFilePath);

            FileUtil.copy(srcFile, wsFile, true);
        }

        //复制文件到指定位置


        DaJnmlbEntity daJnmlbEntity = BeanUtil.mapToBean(params, DaJnmlbEntity.class, true);
        daJnmlbEntity.setLjmc( FileUtils.getFilePath()+daWsEntity.getNbxh()+"/" + srcFileName);
        daJnmlbEntity.setNbxh(daWsEntity.getNbxh());
        daJnmlbEntity.setServerxh(wsServerId);
        daJnmlbEntity.setMlbh(1);
        daJnmlbEntity.setMlmc(daWsEntity.getBt());
        daJnmlbService.save(daJnmlbEntity);
        return daWsEntity;
    }
}
